const logger = require('../utils/log.js')
const path = require('path')
const url = require('url')
const querystring = require('querystring')

logger.trace("Entering cheese testing");
logger.debug("Got cheese.");
logger.info("Cheese is Comté.");
logger.warn("Cheese is quite smelly.");
logger.error("Cheese is too ripe!");
logger.fatal("Cheese was breeding ground for listeria.");

console.log(__dirname) // 物理路径
console.log(path.resolve(__dirname, '../'))

const urlString = 'https://www.baidu.com:443/path/index.html?id=2#tag=3'

// console.log(url.parse(urlString))
logger.debug(url.parse(urlString));

const urlObj = {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com:443',
  port: '443',
  hostname: 'www.baidu.com',
  hash: '#tag=3',
  search: '?id=2',
  query: 'id=2',
  pathname: '/path/index.html',
  path: '/path/index.html?id=2',
  href: 'https://www.baidu.com:443/path/index.html?id=2#tag=3'
}

logger.debug(url.format(urlObj))

var a = url.resolve('/one/two/three', 'four')
var b = url.resolve('http://example.com/', '/one')
var c = url.resolve('http://example.com/one', '/two')
var d = url.resolve('http://example.com/one', '../')
logger.debug(a + "," + b + "," + c + "," + d)

const urlParams = new URLSearchParams(url.parse(urlString).search)
logger.debug(urlParams)
logger.debug(urlParams.get('id'))

var query = 'id=3&city=北京&url=https://www.baidu.com'
var query2 = 'id:3/city:北京/url:https://www.baidu.com'
var queryEscape = 'id%3D3%26city%3D%E5%8C%97%E4%BA%AC%26url%3Dhttps%3A%2F%2Fwww.baidu.com'
var queryObj = {id: '2', name: 'tongyi', from: '北京'}

logger.debug(querystring.parse(query))
logger.debug(querystring.escape(query))
logger.debug(querystring.unescape(queryEscape))
logger.debug(querystring.stringify(queryObj, ':', '/')) //key=value, key value
logger.debug(querystring.parse(query2, '/', ':'))

const newQuery = querystring.stringify((queryObj), null, null, {
  encodeURIComponent(string) {
    return querystring.unescape(string)
  }
})
logger.debug(newQuery)
